<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AdminRole extends Model
{
    protected $guarded = [];//不可以注入的字段

    //当前角色的所有权限
    public function permissions()
    {
    	return $this->belongsToMany(\App\AdminPermission::class, 'admin_permission_role', 'role_id', 'permission_id')->withPivot(['role_id', 'permission_id']);

        // return $this->belongsToMany(\App\AdminPermission::class, 'admin_permission_role', 'permission_id', 'role_id')->withPivot(['permission_id', 'role_id']);
    }

    //给角色赋予某个权限
    public function grantPerssion($permission)
    {
    	return $this->permissions()->save();
    }

    //删除某个角色的权限
    public function deletePerssion($permission)
    {
    	return $this->permissions()->detach();
    }

    //判断角色是否有权限
    public function hasPermission($permission)
    {
    	return $this->permissions()->contains($permission);
    }
}
